<template>
	<view class="content">
    <view class="logo">
      <image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-8638907f-6b78-40ed-8ab5-64a825c57ef1/eece9928-800c-4f9e-bc7c-700e317e5840.png" class="image"></image>
      <view class="text">
        嘉易
      </view>
    </view>
		<button type="default" open-type="getUserInfo" lang="zh-CN" :style="changeStyle" @touchstart="start" @touchend="end" @getuserinfo="wxLogin" class="login">微信登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:{
          openID: "",
          nickName: "",
          avatarUrl: "",
          collection: [],
          gender: '',
          support: [],
          avatarUrlArray: [],
          nickNameArray: []
        },
        changeStyle: '',
        state: 0
			}
		},
		methods: {
      start() {
        this.changeStyle = 'background: rgba(1, 204, 25, .8);'
      },
      end() {
        this.changeStyle = 'background: rgba(1, 204, 25, 1);'
      },
			getCode() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: "weixin",
						success: (e) => {
							resolve(e)
						},
						fail: (err) => {
							reject(new Error("获取code失败"))
						}
					})
				})
			},
			getOpenID(code) {
				return uniCloud.callFunction({
					name: "wxLogin",
					data: {
						code
					}
				})
			},
			getUserProfile() {
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						desc: "获取你的昵称、头像、性别、城市",
						success: (e) => {
							resolve(e)
						},
						fail: (err) => {
							reject(new Error("getUserProfile失败"))
						}
					})
				})
			},
      goToExchange() {
          uni.switchTab({
            url: '/pages/exchange/exchange'
          })
      },
      
    async wxLogin() {
      if(this.state == 0) {
        this.state = 1
        uni.showModal({
        		title: "温馨提示",
        		content: "亲，授权微信登录后才能正常使用小程序功能",
        		success: (e) => {
        			if (e.confirm) {
        				this.getUserProfile().then(e => {
                  uni.setStorageSync('jiayi_nickname', e.userInfo.nickName)
                  uni.setStorageSync('jiayi_avatarUrl', e.userInfo.avatarUrl)
                  uni.setStorageSync('jiayi_gender', e.userInfo.gender)
                  this.userInfo.nickName = e.userInfo.nickName
                  this.userInfo.avatarUrl = e.userInfo.avatarUrl
                  this.userInfo.gender = e.userInfo.gender
                  this.getCode().then(e => {
                   return this.getOpenID(e.code)
                  }).then(e => {
                    const {
                    			openid,
                    			token,
                    			tokenExpired
                    		} = e.result;
                    uni.setStorageSync('jiayi_openid', openid)
                   	uni.setStorageSync('jiayi_token', token)
                    this.userInfo.openID = openid
                    this.goToExchange()
                    this.state = 0
                    // 查找是否有该用户
                    uniCloud.callFunction({
                      name: 'getUser',
                      data: {openID: this.userInfo.openID},
                      success: (res) => {
                        // 没有该用户则添加该用户
                        if(res.result.data.length == 0) {
                          uniCloud.callFunction({
                            name: 'addUser',
                            data: this.userInfo,
                          })
                        } else {
                          const data = res.result.data[0]
                          uni.setStorageSync('jiayi_nickname', data.nickName)
                          uni.setStorageSync('jiayi_avatarUrl', data.avatarUrl)
                          uni.setStorageSync('jiayi_gender', data.gender)
                          uni.setStorageSync('jiayi_avatarUrlArray',data.avatarUrlArray)
                          uni.setStorageSync('jiayi_nickNameArray',data.nickNameArray)
                        }
                      }
                    })
                  })
        				})
        			} else {
                this.state = 0
              }
        		}
        	})
        }
      }
		}
	}
</script>

<style lang="scss">
  page {
    background-color: #f4f4f6;
  }
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
    margin-top: 180rpx;
    .logo {
      text-align: center;
      .image {
        width: 194px;
        height: 194px;
      }
      .text {
        font-size: 50px;
        color: #5d5d5d;
        font-weight: 700;
        font-family: 'Courier New', Courier, monospace;
      }
    }
    .login {
      width: 700rpx;
      color: #fff;
      border: 1px solid #01cc19;
      background-color: #01cc19;
      border-radius: 50px;
      margin-top: 80rpx;
    }
	}
</style>

